import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hygf/util/string_extension.dart';

import '../../config/StyleConfig.dart';
import '../../res/HDColors.dart';
import 'logic.dart';

class LoginPage extends StatelessWidget {
  final logic = Get.put(LoginNewLogic());
  final state = Get.find<LoginNewLogic>().state;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // resizeToAvoidBottomInset: false,
      body: SingleChildScrollView(
        physics: ClampingScrollPhysics(),
        child: SizedBox(
          height: MediaQuery.of(context).size.height,
          child: GetBuilder<LoginNewLogic>(builder: (logic) {
            return _content();
          }),
        ),
      ),
    );
  }

  _content() {
    return Stack(
      children: [
        Positioned(
          bottom: 0,
          left: 0,
          right: 0,
          child: Image.asset(
            "assets/images/login/login_bg.webp",
            fit: BoxFit.cover,
          ),
        ),
        Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Stack(
              children: [
                Container(
                  width: double.infinity,
                  height: 240,
                  color: HDColors.lightGreen,
                ),
                Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children:   [
                    Container(
                      padding:  EdgeInsets.only(left: 32,top: 50),
                      child: Text("您好，", style: TextStyle(
                          fontWeight: FontWeight.bold,
                          color: HDColors.black, fontSize: StyleConfig.fontContentSize18)
                      ),
                    ),
                    Container(
                      padding:  EdgeInsets.only(left: 32,top: 12),
                      child: Text("欢迎使用光伏新能源", style: TextStyle(
                          fontWeight: FontWeight.bold,
                          color: HDColors.black, fontSize: StyleConfig.fontContentSize18)
                      ),
                    )
                  ],
                ),
                Image.asset(
                  "assets/images/login/login_page_header.png",
                  width: double.infinity,
                ),
                Positioned(
                  bottom: 0,
                  left: 0,
                  right: 0,
                  child: InkWell(
                    onTap: () {
                      // logic.selUser();
                    },
                    child: Image.asset(
                      "assets/images/login/login_page_center.png",
                      height: 55,
                      alignment: Alignment.center,
                    ),
                  )
                )
              ],
            ),
            Container(
              width: double.infinity,
              height: 45,
              color: "#F0F2F6".toColor(),
              margin: EdgeInsets.fromLTRB(32, 24, 32, 0),
              padding: EdgeInsets.only(left: 12, right: 12),
              child: _getTextField("请输入用户名", state.controllerName,
                  TextInputType.visiblePassword, false, (str) {}),
            ),
            Container(
              width: double.infinity,
              height: 45,
              color: "#F0F2F6".toColor(),
              margin: EdgeInsets.fromLTRB(32, 24, 32, 0),
              padding: EdgeInsets.only(left: 12, right: 12),
              child: _getTextField("请输入登录密码", state.controllerPsd,
                  TextInputType.visiblePassword, true, (str) {
                logic.requestLogin();
              }),
            ),
            Container(
              width: double.infinity,
              height: 45,
              margin: EdgeInsets.fromLTRB(32, 24, 32, 0),
              child: MaterialButton(
                onPressed: () {
                  logic.requestLogin();
                },
                child: Text(
                  "登录",
                  style: TextStyle(color: Colors.white),
                ),
                color: HDColors.green,
                shape: RoundedRectangleBorder(borderRadius: BorderRadius.zero),
              ),
            ),
            Expanded(child: Container()),
            Visibility(
                visible: state.version.isNotEmpty,
                child: Container(
                    width: double.infinity,
                    alignment: Alignment.center,
                    margin: EdgeInsets.only(bottom: 45),
                    child: Text('当前版本号：${state.version}',
                        style: TextStyle(
                            fontSize: 16, color: HDColors.siGrayHint))))
          ],
        ),
      ],
    );
  }

  _getTextField(String hint, TextEditingController controller,
      TextInputType type, bool hide, Function(String) onSubmit) {
    return TextField(
      keyboardType: type,
      onSubmitted: onSubmit,
      controller: controller,
      obscureText: hide,
      style: const TextStyle(
          color: HDColors.black, fontSize: StyleConfig.fontContentSize16),
      cursorColor: HDColors.accent,
      decoration: InputDecoration(
        hintText: hint,
        hintStyle: TextStyle(
            color: "#CBCED5".toColor(),
            fontSize: StyleConfig.fontContentSize16),
        border: const OutlineInputBorder(
          borderSide: BorderSide(
            color: Colors.transparent,
          ),
        ),
        enabledBorder: const OutlineInputBorder(
          borderSide: BorderSide(
            color: Colors.transparent,
          ),
        ),
        disabledBorder: OutlineInputBorder(
          borderSide: BorderSide(
            color: Colors.transparent,
          ),
        ),
        focusedBorder: OutlineInputBorder(
          borderSide: BorderSide(
            color: Colors.transparent,
          ),
        ),
        contentPadding: EdgeInsets.zero,
      ),
    );
  }
}
